package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.ManualDao;
import com.eamois.domain.manual.EnsipEntity;
import com.eamois.domain.manual.Manual;
import com.eamois.domain.manual.ManualEnsip;

public class ManualDaoImpl extends BaseDaoImpl<Manual> implements ManualDao {

	@SuppressWarnings("unchecked")
	@Override
	public Manual findManualByOther(final String name, final String code,
			final String varsion, final EnsipEntity ensipEntity) {
		// TODO Auto-generated method stub
		return (Manual) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(Manual.class);
						criteria.add(Restrictions.eq("name", name));
						criteria.add(Restrictions.eq("code", code));
						criteria.add(Restrictions.eq("varsion", varsion));
						criteria.add(Restrictions
								.eq("ensipEntity", ensipEntity));
						ArrayList<Manual> list = (ArrayList<Manual>) criteria
								.list();
						if (list.size() == 0) {
							return null;
						} else {
							return list.get(0);
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	public Collection<Manual> findManualByOther(final String code,
			final String name, final String varsion, final String state) {
		// TODO Auto-generated method stub
		return (Collection<Manual>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(Manual.class);
						criteria.add(Restrictions
								.like("code", "%" + code + "%"));
						criteria.add(Restrictions
								.like("name", "%" + name + "%"));
						criteria.add(Restrictions.like("varsion", "%" + varsion
								+ "%"));
						if (!state.equals("")) {
							criteria.add(Restrictions.eq("state", state));
						}
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Manual findManualByOther2(final String code, final String name,
			final String varsion, final String state) {
		// TODO Auto-generated method stub
		return (Manual) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(Manual.class);
						criteria.add(Restrictions.eq("code", code));
						criteria.add(Restrictions.eq("name", name));
						criteria.add(Restrictions.eq("varsion", varsion));
						criteria.add(Restrictions.eq("state", state));
						ArrayList<Manual> list = (ArrayList<Manual>) criteria
								.list();
						if (list.size() == 0) {
							return null;
						} else {
							return list.get(0);
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Manual> findManualByEnsip(final ManualEnsip ensip) {
		// TODO Auto-generated method stub
		return (Collection<Manual>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session.createCriteria(Manual.class);
						criteria.add(Restrictions.eq("manualEnsip", ensip));
						ArrayList<Manual> list = (ArrayList<Manual>) criteria.list();
//						if (list.size() == 0) {
//							return null;
//						} else {
							return list;
//						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Manual> findAllManual() {
		// TODO Auto-generated method stub
		return (Collection<Manual>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(Manual.class);
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Manual> findManualByNameAndRemark(final String name,
			final String remark) {
		// TODO Auto-generated method stub
		return (Collection<Manual>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(Manual.class);
						if (name != null && !"".equals(name)) {
							criteria.add(Restrictions.like("name", "%" + name
									+ "%"));
						}
						if (remark != null && !"".equals(remark)) {
							criteria.add(Restrictions.like("remark", "%"
									+ remark + "%"));
						}
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Manual findManualByName(final String name,final String code) {
		// TODO Auto-generated method stub
		return (Manual) this.getHibernateTemplate().execute(new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session.createCriteria(Manual.class);
						if(name!=null){
							criteria.add(Restrictions.eq("name",  name));
						}
						if(code!=null){
							criteria.add(Restrictions.eq("code",  code));
						}
						if(criteria.list().size()==0){
							return null;
						}else{
							return criteria.list().get(0);
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Manual findManualById(final Integer id) {
		// TODO Auto-generated method stub
		return (Manual) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Manual.class);
				if(id!=null){
					criteria.add(Restrictions.eq("id",  id));
				}
				if(criteria.list().size()==0){
					return null;
				}else{
					return criteria.list().get(0);
				}
			}
		});
	}

}
